<?php

class Silk_Module_Virtual_Type extends Silk_Module_Type {

    protected $_type = null;

    public function __construct(Silk_Type $pType){
	$this->_type = $pType;
    }

    public function getTypeMeta(){
	$virtual_metas = array();
	$metas = $this->_type->getTypeMetas();
	foreach ($metas as $meta){
	    $virtual_metas[] = new Silk_Module_Type_Virtual_Meta($meta);
	}
	return $virtual_metas;
    }
    public function getName(){
	return $this->_type->name;
    }
    public function getSlug(){
	return $this->_type->slug;
    }
    public function getDescription(){
	return $this->_type->description;
    }
    public function getVersion(){
	return $this->_type->version;
    }

    public function getAuthor(){
	return $this->_type->author;
    }

    public function getEmail(){
	return $this->_type->email;
    }

    public function getWebsite(){
	return $this->_type->website;
    }

    public function getAssetPath(){
	return  realpath(BASE_DIR . 'public/assets');
    }

    public function getTemplatePath(){
	return realpath(BASE_DIR . 'app/views/templates/virtual');
    }

    protected function _prepareAdmin(Zend_View_Interface $pView){

	if (array_key_exists('sort', $_REQUEST)){
	    $sort = $_REQUEST['sort'];
	} else
	    $sort = 'object_id';

	if (array_key_exists('order', $_REQUEST)){
	    $order = $_REQUEST['order'];
	} else
	    $order = 'ASC';

	if (array_key_exists('limit', $_REQUEST)){
	    $limit = $_REQUEST['limit'];
	} else
	    $limit = 50;

	if (array_key_exists('page', $_REQUEST)){
	    $page = $_REQUEST['page'];
	} else
	    $page = 1;

	$paginator = Silk::getObjectPaginator($this->getSlug(), array(), $sort, $order);

	$paginator->setItemCountPerPage($limit);
	$paginator->setCurrentPageNumber($page);

	$Types = new Silk_Types();
	$type = $Types->fetchBySlug($this->getSlug(), true);

	$pView->moduletype = $this;
	$pView->type = $type;

// 	$pView->assign('type', $this);
	$pView->objects = $paginator;
	$pView->limit = $limit;
	$pView->sort  = $sort;
	$pView->order = $order;


	return $pView;
    }

    protected function prepare(Silk_Object $pObject=null, array $pVars=array(), $pAction=null, Zend_View_Interface $pView=null){
	$view = parent::prepare($pObject, $pVars, $pAction, $pView);

	if ($pAction && $pAction == 'admin'){
	    return $this->_prepareAdmin($view);
	}

	return $view;
    }

    public function getForm($pObject){

// 	Silk::debug('Silk_Module_Virtual_Type.getForm: Called');
	$form = new Zend_Form();
	$form->setElementsBelongTo('object');
	$form->setMethod('post');

	$name = $form->createElement('text', 'name', array('label' => 'Name'));
	$name->setValue($pObject->name);
	$form->addElement($name);

	$slug = $form->createElement('text', 'slug', array('label' => 'Slug'));
	$slug->setValue($pObject->slug);
	$form->addElement($slug);

	$content = $form->createElement('textarea', 'content', array('label' => 'Content'));
	$content->setValue($pObject->content);
	$form->addElement($content);

	$cancel = $form->createElement('submit', 'Cancel');
	$form->addElement($cancel);

	$submit = $form->createElement('submit', 'Submit');
	$form->addElement($submit);

	return $form;
    }
}

?>